﻿/*
 * FindItemController.cs
 * Written by: Shawn Gilleland
 */

using IndicationLayer;
using StorageLayer;
using Supply_Fetch;
using Supply_Fetch.MUILayer;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace CPLayer
{
    class FindItemController
    {
        private DatabaseManager dbMgr;
        private HardwareCommunicationGateway hwCommGate;
        GUI gui;
        int indication_hold_time=6;

        public bool FindItem(Item item)
        {
            Debug.WriteLine("ILUC-FindItem called with argument: " + item.GetName());
            //item has empty name?  finding item failed
            if (!(item.GetName().Length > 0))
                return false;

            List<Drawer> drawers;

             drawers = dbMgr.FindItem(item);

             Debug.WriteLine("" + item.GetName() + " found in drawers:");
             foreach (Drawer d in drawers)
             {
                 Debug.WriteLine("" + d.ToString());
             }

             if (drawers.Count > 0)
             {
                 Debug.WriteLine("ILUC-FindItem returned true.");
                 //This is commented out for testing purposes
                 //this.LocateItem(drawers);
                 return true;
             }
             else
             {
                 Debug.WriteLine("ILUC-FindItem return false.");
                 return false;
             }
        }

        private List<Drawer> ItemDrawerLocator(Item item)
        {
            return dbMgr.FindItem(item);
        }

        public void LocateItem(Item item)
        {

            hwCommGate.ConnectToSerialPort();
            if(hwCommGate.SerialPortIsOpen()){
                if (gui.ThirteenSecondRadioButton.Checked)
                    indication_hold_time = Constants.THIRTEEN_SEC_WAIT;
                else if (gui.SixteenSecondsRadioButton.Checked)
                    indication_hold_time = Constants.SIXTEEN_SEC_WAIT;
                else if (gui.NineteenSecondRadioButton.Checked)
                    indication_hold_time = Constants.NINETEEN_SEC_WAIT;
                else if (gui.TwentytwoSecondRadioButton.Checked)
                    indication_hold_time = Constants.TWENTYTWO_SEC_WAIT;

                gui.ResetProgressBar(indication_hold_time - 1);
                gui.StepProgressBar();
                gui.ledIndicationWorker.RunWorkerAsync(item);
            }else{
                //could not connect to Arduino
                gui.ResetProgressBar(indication_hold_time - 1);
                gui.SpeechActivation_Panel();
            }

            //test
            
        }

        private void ledIndicationWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            List<Drawer> drawers = new List<Drawer>();
            drawers = ItemDrawerLocator((Item)e.Argument);
            hwCommGate.DeactivateAllDrawers();
            hwCommGate.CommunicationWait(100);
            //5 = 13 secs
            //6 = 16 secs
            //7 = 19 secs
            //8 = 22 secs
            for (int i = 1; i < indication_hold_time; i++)
            {
                hwCommGate.ActivateDrawers(drawers);
                hwCommGate.CommunicationWait(2000);
                hwCommGate.DeactivateAllDrawers();
                hwCommGate.CommunicationWait(1000);
                gui.ledIndicationWorker.ReportProgress(i);
            }
            e.Result = (Item)e.Argument;
        }

        private void ledIndicationWorker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            gui.StepProgressBar();
        }

        private void ledIndicationWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            gui.ResetProgressBar(indication_hold_time-1);
            gui.SpeechActivation_Panel();
            hwCommGate.DisposeSerialPort();
            dbMgr.RecordItemRequest(e.Result as Item);
        }
       
        public FindItemController(GUI mainGui)
        {
            gui = mainGui;
            //Background Worker Event Handles: Miguel Sanchez
            gui.ledIndicationWorker.WorkerSupportsCancellation = true;
            gui.ledIndicationWorker.WorkerReportsProgress = true;
            gui.ledIndicationWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(this.ledIndicationWorker_DoWork);
            gui.ledIndicationWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.ledIndicationWorker_RunWorkerCompleted);
            gui.ledIndicationWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.ledIndicationWorker_ProgressChanged);

            dbMgr = new DatabaseManager();
            hwCommGate = new HardwareCommunicationGateway();
        }
    }
}
